package com.lina.controller;


import com.lina.controller.utils.JwtUtil;
import com.lina.dao.LoginDish;
import com.lina.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.IOException;

@Controller
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private LoginDish loginService;

    @PostMapping
    public String  login(User user, RedirectAttributes redirectAttributes) throws IOException {

        Boolean flag = loginService.Select(user.getUsername(), user.getPassword());
        System.out.println(user.getPassword()+user.getUsername());
        // 创建Token:token保存到user对象
        String token = JwtUtil.createToken();
        if (flag==true) {
            redirectAttributes.addAttribute("token", token);
            return "redirect:/books.html";
        }else {
            return "redirect:/login.html";
        }

    }
}
